﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
using System.Data;

namespace MyEngineExercise.我的扩展
{
    public class MyMethods
    {
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="fileFullPath"></param>
        public static void startProcess(String fileFullPath)
        {
            try
            {
                Process process = new Process();
                process.StartInfo.FileName = fileFullPath;
                process.Start();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        /// <summary>
        /// 命名文件时，日期+时间后缀
        /// </summary>
        /// <returns></returns>
        public static string Date_time_suffix()
        {
            return DateTime.Now.ToShortDateString().Replace('/', '_') + "_" + DateTime.Now.ToLongTimeString().Replace(':', '_');
        }



        /// <summary>
        /// 得某文件夹下某文件类型所有的文件
        /// </summary>
        /// <param name="directory">文件夹路径</param>
        /// <param name="pattern">文件类型</param>
        /// <param name="paths">结果输出到此变量</param>
        public static void GetFiles(DirectoryInfo directory, string pattern, IList<String> paths)
        {


            //用法
            //var pathStr = new List<String>();
            //GetFiles(new DirectoryInfo(Environments.GetSpecialFolder(Environments.SpecialFolder.DyFenfu) + "\\导出的图层"), "*.lyr", pathStr);

            if (directory.Exists || pattern.Trim() != string.Empty)
            {

                foreach (FileInfo info in directory.GetFiles(pattern))
                {
                    //result = result + info.FullName.ToString() + ";";
                    //result = result + info.Name.ToString() + ";";
                    paths.Add(info.FullName);
                }

                foreach (DirectoryInfo info in directory.GetDirectories())
                {
                    GetFiles(info, pattern, paths);
                }
            }

        }


        /// <summary>
        /// 判断文件的编码
        /// Detects the byte order mark of a file and returns
        /// an appropriate encoding for the file.
        /// </summary>
        /// <param name="srcFile"></param>
        /// <returns></returns>
        public static Encoding GetFileEncoding(string srcFile)
        {
            // 参考：http://www.west-wind.com/weblog/posts/2007/Nov/28/Detecting-Text-Encoding-for-StreamReader
            // *** Use Default of Encoding.Default (Ansi CodePage)
            Encoding enc = Encoding.Default;
            // *** Detect byte order mark if any - otherwise assume default
            byte[] buffer = new byte[5];

            System.IO.FileStream file = null;
            try
            {
                file = new System.IO.FileStream(srcFile, FileMode.Open);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


            file.Read(buffer, 0, 5);

            file.Close();

            if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)

                enc = Encoding.UTF8;

            else if (buffer[0] == 0xfe && buffer[1] == 0xff ||
                (buffer[0] == 255 && buffer[1] == 254 && buffer[2] == 102 && buffer[3] == 0 && buffer[4] == 100))
                // 第二个表达式是我自己加的

                enc = Encoding.Unicode;

            else if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)

                enc = Encoding.UTF32;

            else if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
                enc = Encoding.UTF7;

            return enc;

        }


        //加载Excel   
        public static DataSet LoadDataFromExcel(string filePath)
        {
            try
            {
                string strConn;
                strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
                OleDbConnection OleConn = new OleDbConnection(strConn);
                OleConn.Open();
                String sql = "SELECT * FROM  [Sheet1$]";//可是更改Sheet名称，比如sheet2，等等   

                OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
                DataSet OleDsExcle = new DataSet();
                OleDaExcel.Fill(OleDsExcle, "Sheet1");
                OleConn.Close();
                return OleDsExcle;
            }
            catch (Exception err)
            {
                MessageBox.Show("数据绑定Excel失败!失败原因：" + err.Message, "提示信息",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
                return null;
            }
        }  


    }
}
